package com.scs.application.modules.sys.service;

import com.scs.application.core.service.IBaseService;
import com.scs.application.modules.sys.entity.SerialRule;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * <p>
 * 编号规则 服务类
 * </p>
 */
public interface SerialRuleService extends IBaseService<SerialRule> {

    /**
     * 使用默认目标生成编号
     *
     * @param ruleCode
     * @return
     */
    default String generateCode(String ruleCode) {
        List<String> codes = batchGenerateCode(getSerialRule(ruleCode), null, 1);
        return codes.isEmpty() ? null : codes.get(0);
    }

    /**
     * 使用指定目标生成编号
     *
     * @param ruleCode
     * @param target
     * @return
     */
    default String generateCode(String ruleCode, String target) {
        target = StringUtils.defaultString(target, null);

        List<String> codes = batchGenerateCode(getSerialRule(ruleCode), target, 1);
        return codes.isEmpty() ? null : codes.get(0);
    }

    /**
     * 批量生成编号
     * @param ruleCode
     * @param count
     * @return
     */
    default List<String> batchGenerateCode(String ruleCode, int count) {
        return batchGenerateCode(getSerialRule(ruleCode), null, count);
    }

    /**
     * 批量生成编号
     * @param ruleCode
     * @param count
     * @return
     */
    default List<String> batchGenerateCode(String ruleCode,String target, int count) {
        return batchGenerateCode(getSerialRule(ruleCode), target, count);
    }

    List<String> batchGenerateCode(SerialRule serialRule, String target, int count);

    /**
     * 根据Code获取规则
     * @param ruleCode
     * @return
     */
    SerialRule getSerialRule(String ruleCode);

    Boolean isSupport(Boolean flagProject);
}
